#pragma once


#include <iostream>
#include <string>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <cstring>

namespace ns_protocol
{
    #define MYSELF 1
    #define SPACE " "
    #define SPACE_LEN strlen(SPACE)

    class Request
    {
    public:
        //1.自主实现"_x _op _y"
        //2.使用现成的方案
        std::string Serialize()
        {
        #ifdef MYSELF
            std::string str;
            str = std::to_string(_x);
            str += SPACE;
            str += _op;
            str += SPACE;
            str += std::to_string(_y);
            return str;

        #else
            std::cout << "TODO" << std::endl;
        #endif

        }

        //"_x _op _y"
        bool Deserialized(const std::string& str)
        {
        #ifdef MYSELF
            std::size_t left = str.find(SPACE);
            if (left == std::string::npos)
                return false;
            std::size_t right = str.rfind(SPACE);
            if (right == std::string::npos)
                return false;
            _x = atoi(str.substr(0, left).c_str());
            _y = atoi(str.substr(right + SPACE_LEN).c_str());
            if (left + SPACE_LEN > str.size())
                return false;
            else
                _op = str[left + SPACE_LEN];
            return true;

        #else
            std::cout << "TODO" << std::endl;
        #endif
        }

    public:
        Request()
        {}

        Request(int x, int y, char op)
            :_x(x),
            _y(y),
            _op(op)
        {}

        ~Request()
        {}
    public:
        int _x;
        int _y;
        char _op;// + - * / %
    };

    class Response
    {
    public:
        //"_code _result"
        std::string Serialize()
        {
        #ifdef MYSELF
            std::string s;
            s = std::to_string(_code);
            s += SPACE;
            s += std::to_string(_result);

            return s;


        #else
            std::cout << "TODO" << std::endl;
        #endif

        }

        bool Deserialized(const std::string& s)
        {
        #ifdef MYSELF
            std::size_t pos = s.find(SPACE);
            if (pos == std::string::npos)
                return false;
            _code = atoi(s.substr(0, pos).c_str());
            _result = atoi(s.substr(pos + SPACE_LEN).c_str());
            return true;


        #else
            std::cout << "TODO" << std::endl;
        #endif
        }

    public:
        Response()
        {}

        Response(int result, int code)
            :_result(result),
            _code(code)
        {}

        ~Response()
        {}
    public:
        int _result;//计算结果
        int _code;//计算结果的状态码
    };


    std::string Recv(int sock)
    {
        char inbuffer[1024];
        ssize_t s = recv(sock, inbuffer, sizeof(inbuffer), 0);
        if (s > 0)
        {
            return inbuffer;
        }
    }


    void Send(int sock, const std::string str)
    {
        send(sock, str.c_str(), str.size(), 0);
    }




}